package com.zjt.service.product;

import com.zjt.dao.product.IProductInfoDao;
import com.zjt.entity.product.ProductInfo;
import com.zjt.utils.DateUtil;
import com.zjt.web.api.vo.base.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/**
 * Created with IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-29
 * Time: AM9:39
 * 产品相关的接口实现
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ProductService implements IProductService {
    private static Logger logger = LoggerFactory.getLogger(CodeService.class);
    private IProductInfoDao productInfoDao;
    @Autowired
    public void setProductInfoDao(IProductInfoDao productInfoDao) {
        this.productInfoDao = productInfoDao;
    }
    @Override
    public ProductInfo getProductInfoByBarCode(String barCode) {
        return this.productInfoDao.findByCodeValueOrGtin(barCode,barCode);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ProductInfo getProductInfoById(long productId) {
        return this.productInfoDao.findOne(productId);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ProductInfo getProductInfoByGtin(String gtin) {
        return this.productInfoDao.findByCodeValueOrGtin(gtin,gtin);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ProductInfo saveProductInfo(String gtin, String itemName, long brandId, String specification,
                                       String shortDescription, String description, String height, String width,
                                       String depth, String netContent, String grossWeight, String netWeight,
                                       String packagingTypeCode, String packagingMaterialCode, String classCode,
                                       String extension, int flag) {
        //TODO 产品入库
        ProductInfo productInfo = new ProductInfo();
        productInfo.setExtension(extension);
        productInfo.setCreateDate(DateUtil.getCurrentDate());
        productInfo.setUpdateDate(DateUtil.getCurrentDate());
        productInfo.setEnableFlag("Y");
        productInfo.setDepth(depth);
        productInfo.setBrandId(brandId);
        productInfo.setClassCode(classCode);
        productInfo.setDescription(description);
        productInfo.setGrossWeight(grossWeight);
        productInfo.setGtin(gtin);
        productInfo.setHeight(height);
        productInfo.setItemName(itemName);
        productInfo.setNetContent(netContent);
        productInfo.setPackagingMaterialCode(packagingMaterialCode);
        productInfo.setShortDescription(shortDescription);
        productInfo.setPackagingTypeCode(packagingTypeCode);
        productInfo.setWidth(width);
        productInfo.setNetWeight(netWeight);
        productInfo.setProductMaxIndex(1);
        productInfo = this.productInfoDao.save(productInfo);
        return productInfo;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<ProductInfo> getProductInfoListForQRCodePNG(PageVO pageVO) {
        Sort.Direction direction =  Sort.Direction.DESC;
        PageRequest pageRequest = new PageRequest(pageVO.getPageNo(),pageVO.getPageSize(),direction,"id");
        Page<ProductInfo> list = this.productInfoDao.findByProductMaxIndexLessThan(2,pageRequest);
        List<ProductInfo> returnList = new ArrayList<ProductInfo>() ;
        if (list!=null&&list.getNumberOfElements()>0)
        {
            for (ProductInfo productInfo:list)
            {
                returnList.add(productInfo);
            }
        }
        return returnList;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Page<ProductInfo> getAllProductInfoList(PageVO pageVO) {
        Sort.Direction direction =  Sort.Direction.DESC;
        PageRequest pageRequest = new PageRequest(pageVO.getPageNo(),pageVO.getPageSize(),direction,"id");
        Page<ProductInfo> list = this.productInfoDao.findAll(pageRequest);
        return list;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
